package com.xiaoshuidi.cloud.module.rooms.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.common.exception.enums.GlobalErrorCodeConstants;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomequipmentsupplier.vo.*;
import com.xiaoshuidi.cloud.module.rooms.convert.equipmentsupplier.EquipmentSupplierConvert;
import com.xiaoshuidi.cloud.module.rooms.mapper.EquipmentSupplierMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.EquipmentSupplier;
import com.xiaoshuidi.cloud.module.rooms.repository.RoomEquipmentRepository;
import com.xiaoshuidi.cloud.module.rooms.repository.irepository.IEquipmentSupplierRepository;
import com.xiaoshuidi.cloud.module.rooms.service.IRoomEquipmentSupplierService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: Link
 * @CreateTime: 2023-05-18 11:45
 * @Version: 1.0
 */
@Slf4j
@Service
public class RoomEquipmentSupplierServiceImpl extends ServiceImpl<EquipmentSupplierMapper, EquipmentSupplier> implements IRoomEquipmentSupplierService {

    @Resource
    private IEquipmentSupplierRepository iEquipmentSupplierRepository;
    @Resource
    private RoomEquipmentRepository roomEquipmentRepository;

    @Override
    public PageResult<EquipmentSupplierRespVO> getEquipmentSupplierPage(EquipmentSupplierPageReqVO pageVO) {
        PageResult<EquipmentSupplier> list = iEquipmentSupplierRepository.pageQuery(pageVO);
        List<EquipmentSupplierRespVO> pageList = list.getList().stream().map(s -> {
            EquipmentSupplierRespVO resp = new EquipmentSupplierRespVO();
            BeanUtils.copyProperties(s, resp);

            return resp;
        }).collect(Collectors.toList());
        PageResult<EquipmentSupplierRespVO> page = PageResult.empty();
        page.setList(pageList);
        page.setTotal(list.getTotal());
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createEquipmentSupplier(EquipmentSupplierCreateReqVO createReqVO) {
        //校验是否名称存在
        iEquipmentSupplierRepository.validateSupplierName(createReqVO.getName());
        // 插入
        EquipmentSupplier equipmentSupplier = EquipmentSupplierConvert.INSTANCE.convert(createReqVO);
        iEquipmentSupplierRepository.save(equipmentSupplier);
        // 返回
        return equipmentSupplier.getId();
    }

    @Override
    public void updateEquipmentSupplier(EquipmentSupplierUpdateReqVO updateReqVO) {

        if (!updateReqVO.getOldName().equals(updateReqVO.getName())) {
            //校验是否名称存在
            iEquipmentSupplierRepository.validateSupplierName(updateReqVO.getName());
        }
        // 更新
        EquipmentSupplier updateObj = EquipmentSupplierConvert.INSTANCE.convert(updateReqVO);
        iEquipmentSupplierRepository.updateById(updateObj);
    }

    @Override
    public EquipmentSupplier getEquipmentSupplier(Long id) {
        return iEquipmentSupplierRepository.getById(id);
    }

    @Override
    public void deleteEquipmentSupplier(Long id) {
        // 校验存在
        validateEquipmentSupplierExists(id);
        // 删除
        iEquipmentSupplierRepository.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public ImportResp importRoomEquipmentSupplier(List<EquipmentSupplierImportVO> importVOList) {

        if (CollUtil.isEmpty(importVOList)) {
            throw new IllegalArgumentException("导入供应商不能为空！");
        }
        ImportResp respVO = ImportResp.builder().createList(new ArrayList<>())
                .updateList(new ArrayList<>()).failureList(new LinkedHashMap<>()).build();

        List<EquipmentSupplier> list = EquipmentSupplierConvert.INSTANCE.convertList2(importVOList);

        for (EquipmentSupplierImportVO importVO : importVOList) {
            // 校验，判断是否有不符合的原因
        }
        iEquipmentSupplierRepository.saveBatch(list);

        return respVO;

    }

    private void validateEquipmentSupplierExists(Long id) {
        if (iEquipmentSupplierRepository.getById(id) == null) {
            log.error("供应商不存在:id=" + id);
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "供应商不存在");
        }
        Long count = roomEquipmentRepository.countBySupplier(id);
        if (count > 0) {
            throw new ServiceException(GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR.getCode(), "该供应商下存在资产,不可删除");
        }
    }
}
